package com.ruantu.clothing.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruantu.clothing.dao.ProductionDetailsMapper;
import com.ruantu.clothing.dao.RukuMapper;
import com.ruantu.clothing.dao.TailorChildrenMapper;
import com.ruantu.clothing.dao.TailorMapper;
import com.ruantu.clothing.entity.*;
import com.ruantu.clothing.service.TailorService;
import com.ruantu.clothing.util.MyException;
import com.ruantu.clothing.util.UUIDUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by Administrator on 2018/6/19 0019.
 */
@Service
@Transactional
public class TailorServiceImpl implements TailorService {

    @Autowired
    private TailorMapper tailorMapper;

    @Autowired
    private ProductionDetailsMapper productionDetailsMapper;

    @Autowired
    private TailorChildrenMapper tailorChildrenMapper;
    @Autowired
    private RukuMapper rukuMapper;

    @Override
    public List<Tailor> findList(Tailor tailor, PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        if (tailor.getProduct() != null && !"".equals(tailor.getProduct())) {
            map.put("product", tailor.getProduct());
        }
        if (tailor.getState() != null && tailor.getState() != -1) {
            map.put("state", tailor.getState());
        }
        if (StringUtils.isNotBlank(tailor.getTiepai())) {
            map.put("tiepai", tailor.getTiepai());
        }

        if (StringUtils.isNotBlank(tailor.getStartTime())) {
            map.put("startTime", tailor.getStartTime());
            //要包括当天的所以拼接23：59:59
        }
        if (StringUtils.isNotBlank(tailor.getEndTime())) {
            map.put("endTime", tailor.getEndTime() + " 23:59:59");
        }
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        pageBean.setTotal(tailorMapper.findListCount(map));
        List<Tailor> tailors = tailorMapper.findList(map);
        if (tailors.size() > 0) {
            StringBuffer tailorId = new StringBuffer();
            for (int i = 0; i < tailors.size(); i++) {
                if (i == tailors.size() - 1) {
                    tailorId.append("'" + tailors.get(i).getId() + "'");
                    break;
                }
                tailorId.append("'" + tailors.get(i).getId() + "',");
            }
            map.put("tailorId", tailorId.toString());
            List<Tailor> list = tailorMapper.findRukushuByTailorId(map);
            //如果没有就直接返回值
            if (list.size() == 0) {
                return tailors;
            }
            StringBuffer danjuId = new StringBuffer();
            for (int j = 0; j < list.size(); j++) {
                if (j == list.size() - 1) {
                    danjuId.append("'" + list.get(j).getDanjuId() + "'");
                } else {
                    danjuId.append("'" + list.get(j).getDanjuId() + "',");
                }
            }
            map.put("danjuId", danjuId);
            //查询入库退货数量
            List<Ruku> tuihuo = rukuMapper.findTaiorTui(map);
            if (tuihuo.size() != 0) {
                for (int j = 0; j < list.size(); j++) {
                    for (int i = 0; i < tuihuo.size(); i++) {
                        if (tuihuo.get(i).getProduct().equals(list.get(j).getProduct())) {
                            list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                        }
                    }
                }
            }
            for (int j = 0; j < list.size(); j++) {
                for (int i = 0; i < tailors.size(); i++) {
                    if (tailors.get(i).getId().equals(list.get(j).getId()) && tailors.get(i).getProduct().equals(list.get(j).getProduct())) {
                        tailors.get(i).setYiruku(list.get(j).getYiruku());
                        break;
                    }
                }
            }
        }
        return tailors;
    }

    @Override
    public List<Tailor> findListById(String id, int fenlei) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        //1根据颜色分类//2根据尺码分类
        map.put("fenlei", fenlei);
        List<Tailor> tailors = tailorMapper.findChildrenList(map);
        if (tailors.size() > 0) {
            StringBuffer tailorId = new StringBuffer();
            for (int i = 0; i < tailors.size(); i++) {
                if (i == tailors.size() - 1) {
                    tailorId.append("'" + tailors.get(i).getId() + "'");
                    break;
                }
                tailorId.append("'" + tailors.get(i).getId() + "',");
            }
            map.put("tailorId", tailorId.toString());
            List<Tailor> list = tailorMapper.findRukushuByTailorId(map);
            if (list.size() == 0) {
                return tailors;
            }
            StringBuffer danjuId = new StringBuffer();
            for (int j = 0; j < list.size(); j++) {
                if (j == list.size() - 1) {
                    danjuId.append("'" + list.get(j).getDanjuId() + "'");
                } else {
                    danjuId.append("'" + list.get(j).getDanjuId() + "',");
                }
            }
            map.put("danjuId", danjuId);
            //查询入库退货数量
            List<Ruku> tuihuo = rukuMapper.findTaiorTui(map);
            //如果没有退货
            if (tuihuo.size() != 0) {
                //如果有退货 把退货单的数据放到list.yiruku里面去
                for (int j = 0; j < list.size(); j++) {
                    for (int i = 0; i < tuihuo.size(); i++) {
                        if (tuihuo.get(i).getProduct().equals(list.get(j).getProduct())) {
                            if (fenlei == 0) {
                                list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                                break;
                            }
                            //当按颜色
                            if (fenlei == 1) {
                                if (list.get(j).getColor().equals(tuihuo.get(i).getColor())) {
                                    list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                                    break;
                                }
                            }
                            //当按尺码
                            if (fenlei == 2) {
                                if (list.get(j).getColor().equals(tuihuo.get(i).getColor())
                                        && list.get(j).getSize().equals(tuihuo.get(i).getSize())) {
                                    list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            for (int j = 0; j < list.size(); j++) {
                for (int i = 0; i < tailors.size(); i++) {
                    if (tailors.get(i).getId().equals(list.get(j).getId())
                            && tailors.get(i).getProduct().equals(list.get(j).getProduct())) {
                        //当款号
                        if (fenlei == 0) {
                            tailors.get(i).setYiruku(list.get(j).getYiruku());
                            break;
                        }
                        //当按颜色
                        if (fenlei == 1) {
                            if (tailors.get(i).getColor().equals(list.get(j).getColor())) {
                                tailors.get(i).setYiruku(list.get(j).getYiruku());
                                break;
                            }
                        }
                        //当按尺码
                        if (fenlei == 2) {
                            if (tailors.get(i).getColor().equals(list.get(j).getColor())
                                    && tailors.get(i).getSize().equals(list.get(j).getSize())) {
                                tailors.get(i).setYiruku(list.get(j).getYiruku());
                                break;
                            }
                        }
                    }
                }
            }
        }
        return tailors;
    }

    @Override
    public List<Tailor> findTiepai() {
        return tailorMapper.findTiepai();
    }

    @Override
    public int delTailor(Tailor tailor) {
        int code = tailorMapper.delTailor(tailor);
        if (code <= 0) {
            throw new MyException("删除失败，请确认数据重新尝试");
        }
        return code;
    }

    @Override
    @Transactional
    public int save(String a, Tailor tailor) {
        if (tailor.getId() != null && !"".equals(tailor.getId())) {//修改
            //修改主表
            int code = tailorMapper.updateByPrimaryKey(tailor);
            if (code <= 0) {
                throw new MyException("修改失败，请检查数据重新尝试");
            }
            //删除子表里面的信息
            tailorChildrenMapper.delByTailorId(tailor.getId());
        } else {
            //新增
            //新增主表tailor
            tailor.setId(UUIDUtil.getUUID());
            int code = tailorMapper.saveTailor(tailor);
            if (code <= 0) {
                throw new MyException("保存失败，请检查数据重新尝试");
            }
        }
        JSONArray obj = JSONArray.parseArray(a);// 解析String 成JSONARRy
        List<TailorChildren> tailorChildren = new ArrayList<>();
        for (int i = 0; i < obj.size(); i++) {
            JSONObject jo = (JSONObject) obj.get(i);
            TailorChildren tai = new TailorChildren();
            tai.setShuliang(Integer.parseInt(jo.get("caijianshuliang").toString()));
            //裁剪id放进去
            tai.setTailorId(tailor.getId());
            tai.setId(UUIDUtil.getUUID());
            //根据款号id 颜色 尺码查询cpzbid
            ProductionDetails productionDetails = new ProductionDetails();
            productionDetails.setSize(jo.get("size").toString());
            productionDetails.setColor(jo.get("color").toString());
            productionDetails.setProductionId(jo.get("id").toString());
            String productionDetailsId = productionDetailsMapper.findIdBy(productionDetails);
            if (productionDetailsId != null && !"".equals(productionDetailsId)) {
                tai.setProductionDetailsId(productionDetailsId);
                //把类放到集合里面
                tailorChildren.add(tai);
            } else {
                throw new MyException("数据错误，请刷新重新尝试");
            }
        }
        //批量插入子表
        int codes = tailorChildrenMapper.save(tailorChildren);
        if (codes <= 0) {
            throw new MyException("保存失败，请检查数据重新尝试");
        }
        return codes;
    }

    @Override
    public List<Tailor> findById(Tailor tailor) {
        List<Tailor> tailors = tailorMapper.findById(tailor);
        if (tailors.size() <= 0) {
            throw new MyException("数据异常，请检查数据重新尝试");
        }
        return tailors;
    }

    @Override
    public int saveChengbenjia(Tailor tailor) {
        int code = tailorMapper.updateChengbenjia(tailor);
        if (code <= 0) {
            throw new MyException("修改失败，请确认数据重新尝试");
        }
        return code;
    }

    @Override
    public int overCaijiandan(Tailor tailor) {
        tailor.setState(2);
        int code = tailorMapper.updateState(tailor);
        if (code <= 0) {
            throw new MyException("已全入库失败，请检查数据重新尝试");
        }
        return code;
    }

    @Override
    public List<Tailor> findOverTailor(String q) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(q)) {
            map.put("product", q);
        }
        List<Tailor> tailors = tailorMapper.findOverTailor(map);
        if (tailors.size() > 0) {
            StringBuffer tailorId = new StringBuffer();
            for (int i = 0; i < tailors.size(); i++) {
                if (i == tailors.size() - 1) {
                    tailorId.append("'" + tailors.get(i).getId() + "'");
                    break;
                }
                tailorId.append("'" + tailors.get(i).getId() + "',");
            }
            map.put("tailorId", tailorId.toString());
            map.put("newDan", 0);
            List<Tailor> list = tailorMapper.findRukushuByTailorId(map);
            if (list.size() == 0) {
                return tailors;
            }
            StringBuffer danjuId = new StringBuffer();
            for (int j = 0; j < list.size(); j++) {
                if (j == list.size() - 1) {
                    danjuId.append("'" + list.get(j).getDanjuId() + "'");
                } else {
                    danjuId.append("'" + list.get(j).getDanjuId() + "',");
                }
            }
            map.put("danjuId", danjuId);
            //查询入库退货数量
            List<Ruku> tuihuo = rukuMapper.findTaiorTui(map);
            if (tuihuo.size() != 0) {
                for (int j = 0; j < list.size(); j++) {
                    for (int i = 0; i < tuihuo.size(); i++) {
                        if (tuihuo.get(i).getProduct().equals(list.get(j).getProduct())) {
                            list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                        }
                    }
                }
            }
            for (int j = 0; j < list.size(); j++) {
                for (int i = 0; i < tailors.size(); i++) {
                    if (tailors.get(i).getId().equals(list.get(j).getId()) && tailors.get(i).getProduct().equals(list.get(j).getProduct())) {
                        tailors.get(i).setYiruku(list.get(j).getYiruku());
                        break;
                    }
                }
            }
        }
        return tailors;
    }


}
